package org.bugeden.netdisk.matcher

import org.bugeden.netdisk.transfer.Block
import org.hamcrest.{Description, TypeSafeMatcher}

/**
 * Created by IntelliJ IDEA.
 * User: inotseeyou
 * Date: 2010-11-12
 * Time: 20:56:49
 * To change this template use File | Settings | File Templates.
 */

class BlockMatcher extends TypeSafeMatcher[Block]{

  private var block: Block = null

  def setBlockToCompared(b: Block): this.type = {
    this.block = b
    this
  }

  private var compare = (a: Block, b: Block) => true
  def setComparedMethod(cmp: (Block, Block) => Boolean){
    this.compare = cmp
  }

  def describeTo(desc: Description): Unit = {
    desc.appendText("block with id: ").appendValue(block.id)
  }

  def matchesSafely(b: Block): Boolean = {
    compare(block, b)
  }
}

object BlockMatcher{
  def apply(block: Block, cmp: (Block, Block) => Boolean): BlockMatcher = {
    val m = new BlockMatcher
    m.setBlockToCompared(block)
    m.setComparedMethod(cmp)
    m
  }
}